package com.bingo.transaction;

import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.messaging.Message;

@RocketMQTransactionListener(txProducerGroup = TxProducerListener.TX_PRODUCER_GROUP)
public class TxProducerListener implements RocketMQLocalTransactionListener {

    public static final String TX_PRODUCER_GROUP = "Base-Transaction";

    @Override
    public RocketMQLocalTransactionState executeLocalTransaction(Message message, Object o) {
        String time = (String) message.getHeaders().get(RocketMQHeaders.PREFIX + RocketMQHeaders.KEYS);
        String uuid = (String) message.getHeaders().get("uuid");
        try {
            System.out.println("time," + time);
            System.out.println("uuid," + uuid);
            System.out.println(Thread.currentThread().getId() + "---" + Thread.currentThread().getName());
            LocalTxArg localTxArg = (LocalTxArg) o;
            System.out.println("COMMIT," + new Integer(localTxArg.getId()));
            return RocketMQLocalTransactionState.COMMIT;
        } catch (Exception e) {
            MQUtils.put(uuid, e.getMessage());
            System.out.println("ROLLBACK," + e.getMessage());
            return RocketMQLocalTransactionState.ROLLBACK;
        }
    }

    @Override
    public RocketMQLocalTransactionState checkLocalTransaction(Message message) {
        System.out.println("checkLocalTransaction," + message);
        return RocketMQLocalTransactionState.COMMIT;
    }
}
